
import sys, os, re
import urllib
import StringIO
from lxml import etree
from dateutil import parser as dateparser
import cPickle
import bsddb as bdb
from baseHandler import *
import linkArchived


class CdlibHandler(BaseProxyHandler):

    def fetch_changes(self, req, requri, dt=None):

        cxn = bdb.db.DB()
        cxn.open('cdlib.bdb')
	changes = []
        val = cxn.get(requri)
        if(val == None):
          val = cxn.get(requri + "/")
	if(val != None):
        	
	       lval = cPickle.loads(val)
	       for memento in lval.mementos:
			dateobj = dateparser.parse(memento[1] + " GMT")
   	 		changes.append((dateobj, memento[0], {'last' : dateobj, 'obs' : 1, 'type' : 'observed'}))                       
	       changes.sort()
	return changes


def handler(req):
    hdlr = CdlibHandler('cdlib')
    return basehandler(req, hdlr)


